#include "JospFilter.h"
#include "x_string.h"
#include "MuxFactory.h"

const int RATE = 1024;

const	char *res_buff = "JOSP/1.0 200 OK\r\n\r\n";

CJospFilter::CJospFilter()
{
    m_muxFilter = NULL;
    memset(m_strResid, 0, sizeof(m_strResid));
}

CJospFilter::~CJospFilter()
{
    if (m_muxFilter)
        CMuxFactory::Instance()->DelMux(this);
}

int CJospFilter::Parser(const char *request)
{
    CXString x_string(request);

    CXInteger32 i_command("cmd=", "&");
    x_string >> i_command;
    m_nCommandType = i_command();

    CXChar_n c_resid("resid=", "&");
    x_string >> c_resid;
    memcpy(m_strResid, c_resid(), strlen(c_resid()));

    m_nStreamType = 0;
    CXInteger32 i_type("type=", "&");
    x_string >> i_type;
    m_nStreamType = i_type();

    if (strstr(request, "vod") != NULL)
    {
        CXInteger64U u_start("start=", "&");
        x_string >> u_start;
        m_beginTime = u_start();

        CXInteger64U u_end("end=", "&");
        x_string >> u_end;
        m_endTime = u_end();

        if (strstr(request, "scale=") != NULL)
        {
            CXInteger32 i_scale("scale=", "&");
            x_string >> i_scale;
            m_scale = i_scale();
        }
    }
    m_muxFilter = CMuxFactory::Instance()->GetMux(this, "jos");

    return J_OK;
}

int CJospFilter::Convert(const char *pInputData, J_StreamHeader &streamHeader, char *pOutputData, int &nOutLen)
{
    return m_muxFilter->Convert((const char *)pInputData, streamHeader, pOutputData, nOutLen, (void *)&RATE);
}

int CJospFilter::GetRetrunVal(char *pRetVal, int &nRetLen)
{
    int nLen = strlen(res_buff);
    memcpy(pRetVal, res_buff, nLen);
    nRetLen = nLen;

    return J_OK;
}
